const query = require('../util/sql')
const fs = require('fs')
// const path = require('path')
module.exports = async (ctx, next) => {
  const newsId = ctx.request.query.newsId
  const filename = (newsId + '').padStart(5, '0') + '.json'
  sql =`select * from news where id > ${parseInt(newsId)} and isDel=0 ORDER BY id LIMIT 0,1`;
	let nextData = await query(sql);
	sql =`select * from news where id < ${parseInt(newsId)} and isDel=0 ORDER BY id LIMIT 0,1`;
	let prevData = await query(sql);
  let data = {nextData: nextData[0], prevData: prevData[0]}
  if (fs.existsSync(`./dist/json/news/${filename}`)) {
      ctx.state.data = {...JSON.parse(fs.readFileSync(`./dist/json/news/${(newsId + '').padStart(5, '0')}.json`, 'utf-8')), ...data}
      return
  }
  ctx.state.data = data
}